package cj.web.prize.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.constants.MemuEnum;
import cj.web.prize.dao.PrizeResultDAO;
import cj.web.prize.domain.PrizeResult;

/**
 * prize 奖品
 * @author Administrator
 *
 */
@Path("/prizeMan")
public class PrizeAdminController {

	private static Logger logger = Logger.getLogger(PrizeAdminController.class
			.getName());

	private int mStart = 0;
	private int mIndex = 0;
	private String mActive = "";

	@Autowired
	private PrizeResultDAO activeManagementDao;

	@Get("showActive")
	public String showActive(Model model) {
		model.add("MemuEnum",MemuEnum.jpglzjmd);
		return "/views/prize/activeuserList.vm";
	}

	@Get("modAM")
	public String modAM(@Param("phone") String phone,
			@Param("deviceId") String deviceId, @Param("status") int status,
			@Param("remark") String remark) {
		JSONObject jopublic = new JSONObject();
		try {
			jopublic.put("returnFlag", 0);
			jopublic.put("returnMsg", "成功");
			activeManagementDao.updateState(deviceId, phone, status,
					Constants.Activity.ActivityId_5);
			if (status == 2) {
				activeManagementDao.updateCount(remark, mActive);
			}
		} catch (Exception e) {
			jopublic.put("returnFlag", -1);
			jopublic.put("returnMsg", "失败");
		}
		return "@" + jopublic.toString();
	}

	@Post("queryAM")
	public String queryAM(PrizeResult prizeResult) {
		JSONObject jopublic = new JSONObject();
		try {
			prizeResult.setActiveId(Integer.parseInt(Constants.Activity.ActivityId_5));
			List<PrizeResult> objs = activeManagementDao.queryBypd(prizeResult);
			jopublic.put("returnFlag", 0);
			jopublic.put("returnMsg", "成功");
			List<Map> reps = new ArrayList<Map>();
			for (PrizeResult obj : objs) {
				Map rep = new HashMap();
				rep.put("Phone", obj.getPhone());
				rep.put("DeviceID", obj.getDeviceId());
				rep.put("CreateTime", obj.getCreateAt());
				rep.put("StatusCode", obj.getStatus());
				rep.put("Status", GetState(obj.getStatus()));
				int t = Integer.parseInt(obj.getRemark());
				rep.put("RemarkCode", t);
				rep.put("Remark", Winning(t));
				mActive = obj.getActiveId() + "";
				reps.add(rep);
			}
			int size = activeManagementDao.queryRSTotalBypd(prizeResult);
			
			jopublic.put("data", reps);
			jopublic.put("draw", prizeResult.getDraw());
			jopublic.put("recordsTotal", size);
			jopublic.put("recordsFiltered", size);
		} catch (Exception e) {
			jopublic.put("returnFlag", -1);
			jopublic.put("returnMsg", "失败");
		}
		return "@" + jopublic.toString();
	}

	public String Winning(int remark) {
		String temp = "";
		switch (remark) {
		case 1:
			temp = "亨吉利定制金怀表";
			break;
		case 2:
			temp = "亨吉利商务套餐";
			break;
		// case 3:
		// temp="“甜蜜蜜现场饼干烘焙”8折券";
		// break;
		// case 4:
		// temp="“武汉公园客”明信片5折券";
		// break;
		// case 5:
		// temp="创意食盘8折券";
		// break;
		// case 6:
		// temp="文艺帆布包8折券";
		// break;
		// case 7:
		// temp="康师傅糕饼一包";
		// break;
		default:
			temp = "未中奖";
			break;
		}
		return temp;
	}

	public String GetState(int state) {
		String temp = "";
		switch (state) {
		case 0:
			temp = "未领奖";
			break;
		case 1:
			temp = "已领奖";
			break;
		case 2:
			temp = "已无效";
			break;
		case -1:
			temp = "未中奖";
			break;
		default:
			temp = "";
			break;
		}
		return temp;
	}
}